package com.usermanage.controller;

import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.CompanyService;
import com.usermanage.Service.ContractService;
import com.usermanage.Service.MessageService;
import com.usermanage.Service.UserService;
import com.usermanage.beanParam.CompanyBean;
import com.usermanage.beanParam.MessageBean;
import com.usermanage.beanParam.PermissionBean;
import com.usermanage.beanParam.UserBean;
import com.usermanage.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @Resource
    private UserService userService;

    @Resource
    private CompanyService companyService;


    @RequestMapping("/index.page")
    public ModelAndView MQPage() {
        ModelAndView mv = new ModelAndView();
        //获得用户登陆信息
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        //从用户登陆信息中获取用户权限列表
        List<PermissionBean> permissionBeanList = userDetail.getList();
        boolean reports = false;
        boolean order = false;
        boolean sku = false;
        boolean spu = false;
        boolean nsl = false;
        boolean contract = false;
        for (PermissionBean bean : permissionBeanList) {
            if (bean.getPermission_url().equals("/message/index.page")) {
                reports = true;
            }
            if (bean.getPermission_url().equals("/reports/order.page")) {
                order = true;
            }
            if (bean.getPermission_url().equals("/reports/sku.page")) {
                sku = true;
            }
            if (bean.getPermission_url().equals("/reports/spu.page")) {
                spu = true;
            }
            if (bean.getPermission_url().equals("/reports/nsl.page")) {
                nsl = true;
            }
            if (bean.getPermission_url().equals("/reports/contract.page")) {
                contract = true;
            }
        }
        mv.addObject("reports", reports);
        mv.addObject("order", order);
        mv.addObject("sku", sku);
        mv.addObject("spu", spu);
        mv.addObject("nsl", nsl);
        mv.addObject("contract", contract);
        mv.setViewName("/message/index");
        return mv;
    }

    @RequestMapping("/msgIndex.page")
    public ModelAndView MSGIndexPage() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/message/MsgList");
        return mv;
    }

    //消息列表
    @RequestMapping(value = "/list.json", method = RequestMethod.POST)
    public JsonData MQList(String page, String size) throws Exception {
        if (StringUtils.isBlank(page)) {
            page = StaticUtil.pageIndex;
        }
        if (StringUtils.isBlank(size)) {
            size = StaticUtil.sizeCount;
        }
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        List<MessageBean> list = messageService.getAllList(userDetail.getId().toString(), page, size);
        for (MessageBean messageBean : list) {
            UserBean userBean = userService.findUserDetail(String.valueOf(messageBean.getMessage_creater()));
            if (userBean == null) {
                messageBean.setCreater_company("");
                messageBean.setCreater_dept("");
            } else {
                Optional<CompanyBean> optional = companyService.findById(Integer.valueOf(userBean.getCompany_id()));
                messageBean.setCreater_company(optional.get().getCompany_name());
                messageBean.setCreater_dept(userBean.getUser_dept());
            }
            messageBean.setCreate_date(DateUtil.formatDateByDate(messageBean.getMessage_create_date()));
        }
        int count = messageService.getAllListCount(userDetail.getId().toString());
        Map resultMap = new HashMap();
        resultMap.put("list", list);
        resultMap.put("total", count);
        return JsonData.success(resultMap);
    }

    //将消息标记为已读
    @RequestMapping(value = "/updateRead.json")
    public JsonData updateRead(String message_code) {
        try {
            messageService.updateRead(message_code);
            return JsonData.success();
        } catch (Exception e) {
            return JsonData.fail(e.toString());
        }
    }

    //未读消息记录数
    @RequestMapping("/unReadCount.json")
    public JsonData unReadCount() {
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        int count = messageService.userUnReadCount(userDetail.getId().toString());
        return JsonData.success(count);
    }


    //新增一条消息
    @RequestMapping(value = "/insert.json", method = RequestMethod.POST)
    public JsonData insertApp(MessageBean messageBean) throws Exception {
        log.info("接收到的消息参数：" + StringUtil.ObjectToJson(messageBean));
        messageService.send(messageBean);
        return JsonData.success();
    }

    //新增一条消息
    @RequestMapping(value = "/in/insert.json", method = RequestMethod.POST)
    public JsonData insert(MessageBean messageBean) throws Exception {
        log.info("接收到的消息参数：" + StringUtil.ObjectToJson(messageBean));
        messageService.send(messageBean);
        return JsonData.success();
    }


    @RequestMapping("/kInventory.page")
    public ModelAndView kInventory() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("url", StaticUtil.SKUIP);
        mv.setViewName("/message/kInventory");
        return mv;
    }
}
